package com.wn.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;

/**
 * @author yellow Docter
 * @date 2022 -07-05
 * @desc  注解方式配置过滤器
 */
@WebFilter(urlPatterns = "/*",
        initParams = {
            @WebInitParam(name="encoding",value = "utf-8")
        }
)
public class Demo03Filter implements Filter {
    String encoding = "";
    //这初始化
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Demo02Filter.init");
         encoding = filterConfig.getInitParameter("encoding");

    }

    // 过滤 拦截
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("Demo02Filter.doFilter");
        response.setContentType("text/html;charset=utf-8");
        //放行
        chain.doFilter(request, response);
    }

    //销毁
    @Override
    public void destroy() {
        System.out.println("Demo02Filter.destroy");
    }
}
